package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MeetingSystem;
import com.ruoyi.system.service.IMeetingSystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参加会议用户 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-17
 */
@Controller
@RequestMapping("/system/meetingSystem")
public class MeetingSystemController extends BaseController
{
    private String prefix = "system/meetingSystem";
	
	@Autowired
	private IMeetingSystemService meetingSystemService;
	
	@RequiresPermissions("system:meetingSystem:view")
	@GetMapping()
	public String meetingSystem()
	{
	    return prefix + "/meetingSystem";
	}

/**   根据 我登录的手机号 进行 判断 */
	@PostMapping("/chaxun")
	@ResponseBody
	public Map<String,String>  chaxun(Model model,MeetingSystem meetingSystem,HttpServletResponse response,HttpServletRequest request)
	{
	/*	List<MeetingSystem> m=meetingSystemService.selectdui();*/
            //   int rows=0;
        Map<String,String>  map = new HashMap<>();
		MeetingSystem user = meetingSystemService.selectMeeting(meetingSystem);

		if(user ==null){
             //  rows=0;
            map.put("code","1");
            map.put("msg","失败");
        }else{
          //  rows=1;
            request.getSession().setAttribute("user",user);
     /*   int rows=0;
		List<MeetingSystem> mc=meetingSystemService.selectgeren();*/

            /*   System.out.println(mc.size()+"++++++++++");*/
              /*  for(int i=0;i<mc.size();i++){
                    MeetingSystem meeting=mc.get(i);
                    String shoujihao=meeting.getPhone();
                    if (shoujihao.equals(phon)){
                        rows=1;
                        break;
                    }else{
                        rows=0;
                    }
                            *//*if (mc.get(i).getPhone().equals(phon)){
                                rows=1;
                            }else{
                                 rows=0;
                            }*//*
                }*/
            System.out.println(request.getSession().getAttribute("user")+"aaaaaa");

            /*System.out.println(user);*/
            /*to(model,  request, response);*/
            System.out.println("信息走到了 可以看到吗？？");
            map.put("code","2");
            map.put("msg","成功");
        }

		return map ;
	}

	/**
	 * 删除全部参加会议用户
	 */
	/*@Log(title = "参加会议用户", businessType = BusinessType.DELETE)*/
	@GetMapping( "/quanshan")
/*	@ResponseBody*/
	public String quanshan()
	{
		int asdc=meetingSystemService.quanshan();
		return prefix + "/meetingSystem";
	}
	
	/**
	 * 查询参加会议用户列表
	 */
	@RequiresPermissions("system:meetingSystem:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MeetingSystem meetingSystem)
	{
		startPage();
        List<MeetingSystem> list = meetingSystemService.selectMeetingSystemList(meetingSystem);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出参加会议用户列表
	 */
	@RequiresPermissions("system:meetingSystem:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MeetingSystem meetingSystem)
    {
    	List<MeetingSystem> list = meetingSystemService.selectMeetingSystemList(meetingSystem);
        ExcelUtil<MeetingSystem> util = new ExcelUtil<MeetingSystem>(MeetingSystem.class);
        return util.exportExcel(list, "meetingSystem");
    }
	
	/**
	 *  根据点击关闭按钮 进行session 清空 并且退出
	 */
	@GetMapping("/kai")
	public String kai( HttpServletRequest request,HttpServletResponse response)
	{
		request.getSession().removeAttribute("user");
	    return prefix + "/Meetinglogin";
	}

	/**
	 *  扫码跳转页面
	 */
	@GetMapping("/add")
	public String add()
	{
		return prefix + "/add";
	}

	/**
	 *   新增参加会议用户
	 */
	@Log(title = "参加会议用户", businessType = BusinessType.INSERT)
	@PostMapping("/addMerting")
	@ResponseBody
	public Map<String,String>addSave(MeetingSystem meetingSystem, HttpServletRequest request)
	{

		Map<String,String>  map = new HashMap<>();
		String shouji=meetingSystem.getPhone();
		System.out.println(shouji+"_______=+++++++++++++++");
		   MeetingSystem mst=meetingSystemService.selectshouji(shouji);
		   if (mst==null){
			   meetingSystem.setTeamName(meetingSystem.getTeamName().trim());
			   meetingSystem.setNum("HY"+RandomUtils.randomNumFour());
			   meetingSystem.setCreateTime(DateUtils.getNowDate());
			    meetingSystemService.insertMeetingSystem(meetingSystem,request);
		   }else{
			   map.put("code","2");
			   map.put("msg","失败");

		   }



		return  map;
	}


	/**
	 *  扫码跳转页面
	 */
	@GetMapping("/to")
	public String to(Model model, HttpServletRequest request, HttpServletResponse response,MeetingSystem meetingSystem)
	{

		MeetingSystem aa=(MeetingSystem)request.getSession().getAttribute("user");
		    String duiname=aa.getTeamName();
		MeetingSystem hai =meetingSystemService.selectduiwu(duiname);
        List<MeetingSystem> m = meetingSystemService.selectdui( meetingSystem);
		model.addAttribute("zhi",m);

	/*	System.out.println(aa.getUserName()+"+"+aa.getBonusPoints()+"+"+aa.getPoints());*/
		if (aa!=null && hai!=null){
			String duiming=hai.getRank();
			String  duicj=hai.getScores();
			String duiwu =hai.getTeamName();
		      String phone=aa.getPhone();
            System.out.println( phone +"w我的手机号 ——————————");
            MeetingSystem bb  = meetingSystemService.selectshouji(phone);
            model.addAttribute("mingzi",bb.getUserName());/** 这个 是 显示的姓名*/
            System.out.println(bb.getBonusPoints()+"我的 增的分数");
            model.addAttribute("zeng",bb.getBonusPoints());/** 这个 是 增加的分数*/
            model.addAttribute("fu",bb.getPoints());/** 这个 是 减去的分数*/
            model.addAttribute("zong",bb.getTotalCount());/** 这个 是 总分分数*/
            model.addAttribute("paiming",bb.getMing());/** 这个 是 当前排名*/
			model.addAttribute("paiming",bb.getMing());/** 这个 是 当前排名*/
			model.addAttribute("duiming",duiming);
			model.addAttribute("duijc",duicj);
			model.addAttribute("duiwu",duiwu);

		}


  /**prefix + "/webshow"*/
		return prefix + "/webshow";
	}


	/**
	 *  根据手机号登录页面
	 */
	@GetMapping("/bo")
	public String bo(Model model, HttpServletRequest request, HttpServletResponse response,MeetingSystem meetingSystem)
	{
		List<MeetingSystem> m = meetingSystemService.selectdui(meetingSystem);
		System.out.println(m.size()+"数据————————————");

		model.addAttribute("zhi",m);
		MeetingSystem aa=(MeetingSystem)request.getSession().getAttribute("user");
		      String  duiname =aa.getTeamName();/** 获取到当前的 个人的组排名名字*/
		System.out.println(duiname);
		MeetingSystem hai=meetingSystemService.selectduiwu(duiname);/** 获取到 当前的 个人 队组排名*/
		System.out.println(hai.getTeamName());
		if(aa==null){
			return prefix + "/Meetinglogin";
		}else {
			if (aa!=null && hai!=null){
			     String duiming=hai.getRank();
			      String  duicj=hai.getScores();
			     String duiwu =hai.getTeamName();
				long jiafen =aa.getBonusPoints();
				long jianfen=aa.getPoints();
				int mingci=aa.getMing();
				long zongfen=aa.getTotalCount();
				String name=aa.getUserName();

				model.addAttribute("paiming",mingci);
				model.addAttribute("zeng",jiafen);
				model.addAttribute("fu",jianfen);
				model.addAttribute("zong",zongfen);
				model.addAttribute("mingzi",name);
				model.addAttribute("duiming",duiming);
				model.addAttribute("duijc",duicj);
				model.addAttribute("duiwu",duiwu);
			}
			return prefix + "/webshow";
		}

	}
	/**
	 * 修改参加会议用户
	 */
	@GetMapping("/edit/{msId}")
	public String edit(@PathVariable("msId") Long msId, ModelMap mmap)
	{
		MeetingSystem meetingSystem = meetingSystemService.selectMeetingSystemById(msId);
		mmap.put("meetingSystem", meetingSystem);
	    return prefix + "/edit";
	}
	/**
	 * 修改保存参加会议用户
	 */
	@RequiresPermissions("system:meetingSystem:edit")
	@Log(title = "参加会议用户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MeetingSystem meetingSystem)
	{
		meetingSystem.setCreateTime(DateUtils.getNowDate());
		return toAjax(meetingSystemService.updateMeetingSystem(meetingSystem));
	}
	/**
	 * 删除参加会议用户
	 */
	@RequiresPermissions("system:meetingSystem:remove")
	@Log(title = "参加会议用户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(meetingSystemService.deleteMeetingSystemByIds(ids));
	}
	/**
	 *  跳转 批量添加页面  参加会议人数的分数
	 */
	@RequiresPermissions("system:meetingSystem:all")
	@GetMapping( "/toAll")
	public String all(String ids,ModelMap mmap)
	{
		mmap.put("ids",ids);
		return "system/meetingSystem/add1";
	}

	/**
	 *  添加 参加会议人数的分数
	 */
	@RequiresPermissions("system:meetingSystem:remove")
	@Log(title = "参加会议用户", businessType = BusinessType.DELETE)
	@PostMapping( "/batch")
	@ResponseBody
	public AjaxResult batch(String ids,String bonusPoints,String points)
	{
		int row =0;
	   String[] arr = ids.split(",");
		for (String id : arr) {
			MeetingSystem  ms =	meetingSystemService.selectMeetingSystemById(Long.parseLong(id));

			if(ms !=null){
				synchronized (ms){
					if(StringUtils.isNotEmpty(bonusPoints)) {
						ms.setBonusPoints(ms.getBonusPoints() + Long.parseLong(bonusPoints));
						ms.setTotalCount(ms.getTotalCount() + Long.parseLong(bonusPoints));
					}
					if(StringUtils.isNotEmpty(points)) {
						ms.setPoints(ms.getPoints() + Long.parseLong(points));
						ms.setTotalCount(ms.getTotalCount() - Long.parseLong(points));
					}
				}
				row =	meetingSystemService.updateMeetingSystem(ms);
			}
		}
		return toAjax(row);
	}
}
